package com.estragon.jeuxdenfant;

import greendroid.widget.ItemAdapter;
import greendroid.widget.item.LongTextItem;
import greendroid.widget.item.SeparatorItem;
import greendroid.widget.item.TextItem;

import java.util.List;

import android.content.Context;

import com.estragon.sql.DatabaseHelper2;
import com.j256.ormlite.stmt.PreparedQuery;

import core.Animation;
import core.TypeAnimation;

public class AnimationAdapter extends ItemAdapter {

	int idAnimation;
	TypeAnimation typeAnimation;
	

	public AnimationAdapter(Context context,int idAnimation) {
		super(context);
		this.idAnimation = idAnimation;
		try {
			PreparedQuery<TypeAnimation> query = DatabaseHelper2.getHelper().getTypeAnimationDao().queryBuilder().where().eq("id", idAnimation).prepare();
			typeAnimation = DatabaseHelper2.getHelper().getTypeAnimationDao().query(query).get(0);
			PreparedQuery<Animation> query2 = DatabaseHelper2.getHelper().getAnimationDao().queryBuilder().where().eq("type", idAnimation).prepare();
			List<Animation> animations = DatabaseHelper2.getHelper().getAnimationDao().query(query2);
			add(new SeparatorItem(typeAnimation.getNom()));
			for (Animation animation : animations) {
				add(new AnimationItem(animation));
			}
		}
		catch (Exception e) {
			add(new TextItem("Erreur : "+e.toString()));
		}
	}
	
	public static class AnimationItem extends LongTextItem {
		
		Animation animation;
		
		public AnimationItem(Animation animation) {
			this.animation = animation;
			this.text = animation.getNom();
		}
		
		public Animation getAnimation() {
			return animation;
		}

	}
	
	public int getIdAnimationFromPosition(int position) throws AnimationIntrouvableException {
		try {
			AnimationItem item = (AnimationItem) getItem(position);
			return item.getAnimation().getInternalId();
		}
		catch (Exception e) {
			throw new AnimationIntrouvableException();
		}
	}
	
	public static class AnimationIntrouvableException extends Exception {
		
	}

}
